<template>
  <div class="container">
    <div class="result" v-if="show">
      <div v-if="type == 2">&nbsp&nbsp参考答案:{{ result.translation }}</div>
      <div v-if="type == 1">&nbsp&nbsp得分:{{ result.score }}</div>
      <div v-if="type == 1">&nbsp&nbsp批注:{{ result.feedback }}</div>
      <div v-if="type == 1">&nbsp&nbsp建议:{{ result.advice }}</div>
    </div>
    <div v-if="!show">
      <van-grid column-num="4">
        <van-grid-item text="题号" />
        <van-grid-item text="你的答案" />
        <van-grid-item text="正确答案" />
        <van-grid-item text="结果" />
      </van-grid>
      <div v-for="(item, index) in resultList" :key="index">
        <van-grid column-num="4">
          <van-grid-item :text="index + 1" />
          <van-grid-item :text="item.answer" />
          <van-grid-item :text="item.match" />
          <van-grid-item :text="item.result" />
        </van-grid>
      </div>
    </div>

    <div class="button">
      <van-button square type="primary" custom-style="width:100%" @click="home"
        >回到首页</van-button
      >
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  data() {
    return {
      result: {},
      resultList: [],
      show: false,
      type: 1,
    };
  },

  onLoad: function (option) {
    console.log(option);
    if (option.type == 1 || option.type == 2) {
      this.type = option.type;
      this.show = true;
      this.commit(option.type, option.content);
    } else {
      this.show = false;
      this.check(option.answer, option.match);
    }
  },

  methods: {
    async check(answer, match) {
      let params = {};
      params.answer = answer;
      params.match = match;
      console.log(params);
      let response = await request({
        url: `/course_service/course/checkAnswer`,
        method: "POST",
        data: params,
      });
      console.log(response);

      this.resultList = response.data.list;
    },

    async commit(type, content) {
      let response = {};
      if (type == 1) {
        response = await request({
          url: `/exam_service/writing/correct/${content}`,
          method: "GET",
        });
      } else {
        content = encodeURI(content)
        console.log(content)
        try {
          response = await request({
          url: `/exam_service/writing/translate/${content}`,
          method: "GET",
        });
        } catch (error) {
          
        console.log(error)
        }
      }
      console.log(response);
      this.result = response.data;
      console.log(result);
    },

    async home() {
      wx.switchTab({
        url: "/pages/index/main",
      });
    },
  },
};
</script>

<style>
.result {
  font-size: 40rpx;
}
.container {
  position: relative;
  height: 100%;
}
.button {
  position: absolute;
  bottom: 0;
  display: inline-block;
  width: 100%;
}
</style>
